package com.ktjy;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分支流程
 */
@SpringBootTest
public class ActivitiTester2 {
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    TaskService taskService;
    @Autowired
    HistoryService historyService;

    @Test
    public void test(){
        //流程图部署
        Deployment deployment = repositoryService.createDeployment()
                .name("活动申请2.0")
                .addClasspathResource("bpmn/Activity2.bpmn20.xml")
                .deploy();
        System.out.println("部署成功，流程部署ID："+ deployment.getId());
    }

    @Test
    public void test2(){
        //启动流程
        //流程定义id
        String processDefinitionId="Activity:4:bbc469e8-817a-11f0-a52b-8c16454406a2";
        //流程实例
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceById(processDefinitionId);
        System.out.println("启动成功");
    }

    @Test
    public void test3(){
        //查询待办任务
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee("张娜娜").list();

        for (Task task : list) {
            System.out.println("任务ID：" + task.getId());
            System.out.println("流程实例ID：" + task.getProcessInstanceId());
            System.out.println("流程执行ID：" + task.getExecutionId());
            System.out.println("流程定义ID：" + task.getProcessDefinitionId());
            System.out.println("任务名称：" + task.getName());
            System.out.println("任务办理人：" + task.getAssignee());
        }
    }

    @Test
    public void test4(){
        String taskId="d9576ee5-8183-11f0-a190-8c16454406a2";
        Map<String,Object> param=new HashMap<>();
//        param.put("outcome","驳回");
        param.put("outcome","通过");

        taskService.complete(taskId,param);
        System.out.println("完成任务成功");
    }
}

